<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    main {
      width: 1200px;

      margin: 0 auto;
      border: 1px solid blue;
    }

    .m-search {
      padding: 10px;
      border: 1px solid gray;
    }

    .m-cont {
      padding: 10px;
      border: 1px solid gray;
    }

    table {
      width: 100%;
      text-align: center;
    }

    tr {
      height: 40px;
    }

    table,
    td,
    th {
      border: 1px solid orange;
      border-collapse: collapse;
    }
  </style>
</head>

<body>

  <main>
    <div class="m-search">
      姓名: <input type="text" name="name" id="name">
      <button>搜索</button>
      <a href="add.html">添加</a>
    </div>
    <div class="m-cont">
      <table>
        <thead>
          <tr>
            <th>序号</th>
            <th>学号</th>
            <th>姓名</th>
            <th>班级</th>
            <th>性别</th>
            <th>年龄</th>
            <th>电话</th>
            <th>爱好</th>
            <th>地址</th>
            <th>备注</th>
            <th>日期</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>

        </tbody>
      </table>
    </div>
  </main>

  <script>
    // 1.创建XMLHttpRequest对象
    var xhr = new XMLHttpRequest();
    // 2. 添加状态改变的回调函数,
    xhr.onreadystatechange = function () {
      //5 判断请求状态为4
      if (xhr.readyState == 4) {
        // 6 判断响应状态码
        if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {

          var arr = JSON.parse(xhr.response)
          //console.log(arr);
          var str = ``;
          arr.forEach(function (stu, i) {
            str += `
              <tr>
                <td>${i + 1}</td>
                <td>${stu.id}</td>
                <td>${stu.name}</td>
                <td>${stu.clazz}</td>
                <td>${stu.gender}</td>
                <td>${stu.age}</td>
                <td>${stu.tel}</td>
                <td>${stu.hobby}</td>
                <td>${stu.address}</td>
                <td>${stu.remark}</td>
                <td>${stu.date}</td>
                <td>
                  <a href="edit.html">编辑</a>
                  <a href="javascript:;">删除</a>
                </td>
              </tr>
            `
          });
          document.querySelector('tbody').innerHTML = str
        }
      }
    }
    // 3. 打开连接设置请求方式和URL
    xhr.open("GET", "http://localhost:3008/api/student/getstudent")
    // 4. 发送请求
    xhr.send();

  </script>
</body>

</html>